﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Benefit.Models.Sys;
namespace Benefit.Models.Day
{
    public class AccountDayAnalysis
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public int AccountId { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public virtual Account Account { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public int TradeHistoryId { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public virtual TradeHistory TradeHistory { get; set; }

        /// <summary>
        /// 资金利用率
        /// </summary>
        public double Utilization { get; set; }
        /// <summary>
        /// 胜率
        /// </summary>
        public double WinRate { get; set; }
        /// <summary>
        /// 涨幅
        /// </summary>
        public double RaiseRate { get; set; }

        /// <summary>
        /// 每笔盈利
        /// </summary>
        public double AvgBillBenefit { get; set; }

        /// <summary>
        /// 超过止损次数
        /// </summary>
        public double OverStopLoss { get; set; }

        /// <summary>
        /// 风险次数
        /// </summary>
        public int RiskCount { get; set; }

        /// <summary>
        /// 收益率
        /// </summary>
        public double Yield { get; set; }
        /// <summary>
        /// 最大回撤
        /// </summary>
        public double MaxReturn { get; set; }
        /// <summary>
        /// 平均持仓时间
        /// </summary>
        public double AvgPostionTime { get; set; }
        /// <summary>
        /// 平进平出单量
        /// </summary>
        public int PingJinPing { get; set; }

        ///// <summary>
        ///// 删除某日的所有数据
        ///// </summary>
        ///// <param name="tradeHistoryId"></param>
        //public void Delete(int tradeHistoryId, DB.DBManager db)
        //{
        //    var query = from t in db.AccountDayAnalysis where t.TradeHistoryId == tradeHistoryId select t;
        //    foreach (AccountDayAnalysis d in query)
        //    {
        //        db.AccountDayAnalysis.Remove(d);
        //    }
        //    db.SaveChanges();
        //}
        ///// <summary>
        ///// 保存账号日分析情况
        ///// </summary>
        ///// <param name="tradeHistoryId">插入时间编号</param>
        ///// <param name="pdate">插入时间</param>
        //public void InitAccountDayAnalysis(int tradeHistoryId, string pdate, DB.DBManager db)
        //{
        //    List<Data.ServerData.T_Account> serverAccounts = new Data.ServerData.T_Account().GetList();
        //    foreach (Data.ServerData.T_Account account in serverAccounts)
        //    {
        //        Data.Day.AccountDayAnalysis change = new Data.Day.AccountDayAnalysis();
        //        Sys.Account _account = new Sys.Account();
        //        change.AccountId = _account.GetAccountId(account.LoginAccount);
        //        Data.ServerData.T_TradeDetail_History tth = new Data.ServerData.T_TradeDetail_History();
        //        Data.ServerData.T_SettlementMoney money = new ServerData.T_SettlementMoney();
        //        money = money.GetSettlementMoney(account.Id, pdate, db);
        //        int wincount = tth.GetDayWinCount(pdate, account.Id, db);
        //        int losecount = tth.GetDayLoseCount(pdate, account.Id, db);
        //        int pingcount = tth.GetDayPingCount(pdate, account.Id, db);
        //        //if ((wincount + losecount + pingcount) > 0)
        //        //{
        //        int allbillcount = tth.GetDayBillCount(pdate, account.Id, db);
        //        if (allbillcount == 0)
        //        {
        //            change.AvgBillBenefit = 0;
        //        }
        //        else
        //        {
        //            change.AvgBillBenefit = Math.Round((money.CloseProfit - money.Commission) / allbillcount, 2);
        //        }
        //        change.AvgPostionTime = tth.GetAccountAvgPostionTime(pdate, account.Id, change.AccountId, tradeHistoryId, db);
        //        change.MaxReturn = tth.GetMaxReturn(pdate, account.Id, db);
        //        Data.Day.DayWinOrLoseTick tick = new DayWinOrLoseTick();
        //        change.OverStopLoss = tick.GetOverStopLoss(pdate, account.Id, Config.DefaultValue.OverLostTick, db);
        //        change.PingJinPing = pingcount;
        //        change.RaiseRate = money.GetRaiseRate(account.Id, pdate, db);
        //        change.RiskCount = Convert.ToInt32(change.OverStopLoss);
        //        change.TradeHistoryId = tradeHistoryId;
        //        if (money.PresetTlePosit > 0)
        //        {
        //            change.Utilization = Math.Round(tth.GetAllOpenMoney(pdate, account.Id, db) / money.PresetTlePosit, 2);
        //        }
        //        else
        //        {
        //            change.Utilization = 0;
        //        }
        //        double winrate = 0;
        //        if ((wincount + losecount + pingcount) == 0)
        //        {
        //            winrate = 1f;
        //        }
        //        else
        //        {
        //            if (wincount == 0)
        //            {
        //                winrate = 0;
        //            }
        //            else
        //            {
        //                winrate = wincount / (wincount + losecount + pingcount);
        //            }

        //        }

        //        change.WinRate = Math.Round(winrate, 2);
        //        if (money.PresetTlePosit == 0)
        //        {
        //            if ((money.CloseProfit - money.Commission) > 0)
        //            {
        //                change.Yield = Math.Round((money.CloseProfit - money.Commission), 2);
        //            }
        //            else
        //            {
        //                change.Yield = 0;
        //            }

        //        }
        //        else
        //        {
        //            if ((money.CloseProfit - money.Commission) > 0)
        //            {
        //                change.Yield = Math.Round((money.CloseProfit - money.Commission) / money.PresetTlePosit, 2);
        //            }
        //            else
        //            {
        //                change.Yield = 0;
        //            }

        //        }
        //        db.AccountDayAnalysis.Add(change);
        //        //}

        //    }
        //    db.SaveChanges();
        //}


        //public AccountDayAnalysis GetAccountDayAnalysis(int tradeHistoryId, int accountid, DB.DBManager db)
        //{
        //    AccountDayAnalysis returnValue = new AccountDayAnalysis();
        //    var query = db.AccountDayAnalysis.Where(a => a.AccountId == accountid).Where(a => a.TradeHistoryId == tradeHistoryId);
        //    if (query.Count() > 0)
        //    {
        //        returnValue = query.First();
        //    }
        //    return returnValue;
        //}
    }
}